﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Net.Mail;
using eschoolBO;

namespace ESCHOOL.admin
{
    public partial class WebForm3 : System.Web.UI.Page
    {
        student stud = new student();
        parent par = new parent();
        Classes cl = new Classes();
        user_types ut = new user_types();
        protected void Page_Load(object sender, EventArgs e)
        {

            btnupdate.Visible = false;

            if (!IsPostBack)
            {
                for (int i = 1; i < 31; i++)
                    dddate.Items.Add(i.ToString());
                for (int j = 1950; j < 2050; j++)
                    ddyear.Items.Add(j.ToString());

                DataTable dt1 = cl.GetRecords(-99, "", -99, false, " and ");                
                ddclass.DataSource = dt1;
                ddclass.DataTextField = "class_name";
                ddclass.DataValueField = "class_id";
                ddclass.DataBind();

                DataTable dt2 = ut.GetRecords(-99, "", false, " and ");
                ddusertype.DataSource = dt2;
                ddusertype.DataTextField = "user_type";
                ddusertype.DataValueField = "user_type_id";
                ddusertype.DataBind();

                DataTable dt = stud.GetRecords(-99, "", -99, -99, "", "", "", "", "", "", -99, -99, "", -99, "", "","","", false, " and ");
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
        }

        protected void btnedit_Command(object sender, CommandEventArgs e)
        {
            btninsert.Visible = false;
            btnupdate.Visible = true;
            DataTable dt = stud.GetRecords(Convert.ToInt64(e.CommandArgument), "", -99, -99,"", "", "", "", "", "", -99,-99, "", -99, "","","","",false, " and ");
            tbstudentid.Text= dt.Rows[0]["student_id"].ToString();
            tbenroll.Text = dt.Rows[0]["enrollment_no"].ToString();
            tbroll.Text = dt.Rows[0]["roll_no"].ToString();
            ddclass.SelectedValue = dt.Rows[0]["class_id"].ToString();
            DateTime dat1 = new DateTime();
           dat1 = Convert.ToDateTime(dt.Rows[0]["year_of_studing"].ToString());
           ddyearstuding.SelectedValue = dat1.Year.ToString();
            tbstudname.Text = dt.Rows[0]["student_name"].ToString();
            DateTime dat = new DateTime();
            dat = Convert.ToDateTime(dt.Rows[0]["date_of_birth"].ToString());
            dddate.SelectedValue=dat.Day.ToString();
            ddmonth.SelectedValue=dat.Month.ToString();
            ddyear.SelectedValue=dat.Year.ToString();
            RadioButtonList1.SelectedValue = dt.Rows[0]["gender"].ToString();
            TextBox1.Text = dt.Rows[0]["photo"].ToString();
            tbpaddess.Text = dt.Rows[0]["permanent_address"].ToString();
            tbrespno.Text = dt.Rows[0]["residence_pno"].ToString();
            tbmobile.Text = dt.Rows[0]["mobile_no"].ToString();
            tbstudmail.Text = dt.Rows[0]["student_emailid"].ToString();
            ddusertype.SelectedValue = dt.Rows[0]["user_type_id"].ToString();
            tbusername.ReadOnly = true; labusername.Visible = false; tbusername.Visible = false;
            tbpassword.ReadOnly = true; labpassword.Visible = false; tbpassword.Visible = false;
            tbusername.Text = dt.Rows[0]["username"].ToString();
            tbpassword.Text = dt.Rows[0]["password"].ToString();
            tbques.Text = dt.Rows[0]["security_question"].ToString();
            tbanswer.Text = dt.Rows[0]["answer"].ToString();
        }

        protected void btndelete_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                DataTable dt = stud.GetRecords(Convert.ToInt64(e.CommandArgument), "", -99, -99, "", "", "", "", "", "", -99, -99, "", -99, "", "", "","",false, " and ");
                stud.student_id = Convert.ToInt64(dt.Rows[0]["student_id"].ToString());
                stud.enrollment_no = dt.Rows[0]["enrollment_no"].ToString();
                stud.roll_no = Convert.ToInt64(dt.Rows[0]["roll_no"].ToString());
                stud.class_id = Convert.ToInt64(dt.Rows[0]["class_id"].ToString());
                stud.year_of_studing = dt.Rows[0]["year_of_studing"].ToString();
                stud.student_name = dt.Rows[0]["student_name"].ToString();
                stud.date_of_birth = dt.Rows[0]["date_of_birth"].ToString();
                stud.gender = dt.Rows[0]["gender"].ToString();
                stud.photo = dt.Rows[0]["photo"].ToString();
                stud.permanent_address = dt.Rows[0]["permanent_address"].ToString();
                stud.residence_pno = Convert.ToInt64(dt.Rows[0]["residence_pno"].ToString());
                stud.mobile_no = Convert.ToInt64(dt.Rows[0]["mobile_no"].ToString());
                stud.student_emailid = dt.Rows[0]["student_emailid"].ToString();
                stud.user_type_id = Convert.ToInt64(dt.Rows[0]["user_type_id"].ToString());
                stud.username = dt.Rows[0]["username"].ToString();
                stud.password = dt.Rows[0]["password"].ToString();
                stud.security_question = dt.Rows[0]["security_question"].ToString();
                stud.answer = dt.Rows[0]["answer"].ToString();
                stud.isdeleted = true;
                stud.SaveRecords();
                Response.Redirect("~/admin/Student.aspx");
            }
        }

        protected void btninsert_Click(object sender, EventArgs e)
        {
            stud.student_id = -99;
            stud.enrollment_no = tbenroll.Text;
            stud.roll_no = Convert.ToInt64(tbroll.Text);
            stud.class_id = Convert.ToInt64(ddclass.SelectedValue.ToString());
            stud.year_of_studing = ddyearstuding.SelectedValue.ToString();
            stud.student_name = tbstudname.Text;
            DateTime dat = new DateTime(Convert.ToInt32(ddyear.SelectedItem.ToString()), Convert.ToInt32(ddmonth.SelectedValue.ToString()), Convert.ToInt32(dddate.SelectedItem.ToString()));
            stud.date_of_birth = dat.ToString();
            stud.gender = RadioButtonList1.SelectedValue.ToString();
            if (FileUpload1.HasFile)
                FileUpload1.PostedFile.SaveAs(Server.MapPath("~/admin/student/" + FileUpload1.FileName));
            stud.photo = FileUpload1.FileName;
            stud.permanent_address = tbpaddess.Text;
            stud.residence_pno = Convert.ToInt64(tbrespno.Text);
            stud.mobile_no = Convert.ToInt64(tbmobile.Text);
            stud.student_emailid = tbstudmail.Text;
            stud.user_type_id = Convert.ToInt64(ddusertype.SelectedValue.ToString());
            stud.username = tbusername.Text;
            stud.password = tbpassword.Text;
            stud.security_question = tbques.Text;
            stud.answer = tbanswer.Text;
            stud.isdeleted = false;
            stud.SaveRecords();
            //mail to student
            MailMessage MyMessage = new MailMessage();
            MailAddress SendFrom = new MailAddress("eschool123g@gmail.com");
            MyMessage.To.Add(tbstudmail.Text);
            MyMessage.From = SendFrom;
            MyMessage.Subject = "Your login information.";
            MyMessage.Body ="Username:"+ tbusername.Text+" Password: "+tbpassword.Text;
            System.Net.NetworkCredential basicAuthenticationInfo = new System.Net.NetworkCredential("eschool123g@gmail.com", "eschool@123");

            SmtpClient emailClient1 = new SmtpClient("smtp.gmail.com");
            emailClient1.EnableSsl = true;
            emailClient1.Port = 587;
            emailClient1.Credentials = basicAuthenticationInfo;
            emailClient1.Send(MyMessage);


            Response.Redirect("~/admin/Student.aspx");
        }

        protected void btnupdate_Click(object sender, EventArgs e)
        {
            
            stud.student_id = Convert.ToInt64(tbstudentid.Text);
            stud.enrollment_no = tbenroll.Text;
            stud.roll_no = Convert.ToInt64(tbroll.Text);
            stud.class_id = Convert.ToInt64(ddclass.SelectedValue.ToString());
            stud.year_of_studing = ddyearstuding.SelectedValue.ToString();
            stud.student_name = tbstudname.Text;
            DateTime dat = new DateTime(Convert.ToInt32(ddyear.SelectedItem.ToString()), Convert.ToInt32(ddmonth.SelectedValue.ToString()), Convert.ToInt32(dddate.SelectedItem.ToString()));
            stud.date_of_birth = dat.ToString();
            stud.gender = RadioButtonList1.SelectedValue.ToString();
            if (FileUpload1.HasFile)
            {
                FileUpload1.PostedFile.SaveAs(Server.MapPath("~/admin/student/" + FileUpload1.FileName));
                stud.photo = FileUpload1.FileName;
            }
            else
                stud.photo = TextBox1.Text;
            stud.permanent_address = tbpaddess.Text;
            stud.residence_pno = Convert.ToInt64(tbrespno.Text);
            stud.mobile_no = Convert.ToInt64(tbmobile.Text);
            stud.student_emailid = tbstudmail.Text;
            stud.user_type_id = Convert.ToInt64(ddusertype.SelectedValue.ToString());
            stud.username = tbusername.Text;
            stud.password = tbpassword.Text;
            stud.security_question = tbques.Text;
            stud.answer = tbanswer.Text;
            stud.isdeleted = false;
            stud.SaveRecords();
            Response.Redirect("~/admin/Student.aspx");

        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {

        }

        protected void dddate_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void ddmonth_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void ddyear_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void LinkButton1_Command(object sender, CommandEventArgs e)
        {
            //Response.Redirect("~/admin/ParentInfo.aspx");
        }

      
     

        protected void ddclss_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

        }
    }
}
